InboxProvider   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 8
eloc 37
dl 0
loc 44
rs 10
c 0
b 0
f 0

5 Functions

Rating   Name   Duplication   Size   Complexity  
A handleAction 0 7 3
A handleFilter 0 13 2
A setEmails 0 1 1
A setCurrentFilter 0 1 1
A render 0 3 1
1
import React, { Component } from 'react';
2
import { EmailContext } from '../../../context/Context';
3
import rawEmails from '../../../data/email/emails';
4
import { isIterableArray } from '../../../helpers/utils';
5
6
class InboxProvider extends Component {
7
  setEmails = emails => this.setState({ emails });
8
  setCurrentFilter = currentFilter => this.setState({ currentFilter });
9
10
  // Handlers
11
  handleAction = (action, selectedIds) => {
12
    const { emails, setEmails } = this.state;
13
14
    setEmails(
15
      action === 'delete' || action === 'archive'
16
        ? emails.filter(({ id }) => !selectedIds.includes(id))
17
        : emails.map(email => (selectedIds.includes(email.id) ? { ...email, [action]: !email[action] } : email))
18
    );
19
  };
20
21
  handleFilter = filter => {
22
    const { setEmails, setCurrentFilter } = this.state;
23
    setCurrentFilter(filter);
24
25
    switch (filter) {
26
      case 'all':
27
        return setEmails(rawEmails);
28
      case 'unread':
29
        return setEmails(rawEmails.filter(email => !email.read));
30
      case 'attachments':
31
        return setEmails(rawEmails.filter(email => isIterableArray(email.attachments)));
32
      default:
33
        return setEmails(rawEmails.filter(email => email[filter]));
34
    }
35
  };
36
37
  state = {
38
    emails: [],
39
    setEmails: this.setEmails,
40
    filters: ['all', 'unread', 'star', 'attachments', 'archive', 'snooze'],
41
    currentFilter: 'all',
42
    setCurrentFilter: this.setCurrentFilter,
43
    handleFilter: this.handleFilter,
44
    handleAction: this.handleAction
45
  };
46
47
  render() {
48
    return <EmailContext.Provider value={this.state}>{this.props.children}</EmailContext.Provider>;
49
  }
50
}
51
52
export default InboxProvider;
53